<%@ page import="java.time.LocalDateTime" %><%--
  Created by IntelliJ IDEA.
  User: ADMIN
  Date: 2023/3/27
  Time: 21:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>

<%
    request.setCharacterEncoding("utf-8");
    //判断需要处理的逻辑//
    //       state = 1  插入 2/修改 3 删除

    Integer state = Integer.valueOf(request.getParameter("state"));
    String opereation = null;

    String createtime = LocalDateTime.now().toString();
    Integer depart = Integer.valueOf(session.getAttribute("depart").toString());
    String username = session.getAttribute("username").toString();

    Integer id = null;
    String name= null;
    String phone = null;
    String identity = null;
    Integer age = null;
    String price = null;
    String images = null;
    String number = null;
    String introduction = null;
    Integer status = null;
    Integer gearbox = null;
    String address = null;
    Integer collection = null;




    String sql = null;

    if(state == 1){
        price = request.getParameter("price");
        identity = request.getParameter("identity");
        images = request.getParameter("images");
        number = request.getParameter("number");
        introduction = request.getParameter("introduction");
        age =Integer.valueOf( request.getParameter("age"));
        gearbox =Integer.valueOf( request.getParameter("gearbox"));
        name = request.getParameter("name");
        address = request.getParameter("address");
        phone = request.getParameter("phone");

        //管理员无需审核
        if(depart == 11){
            opereation = "上传车辆";
            status = 22;
        }else {
            status = 11;
            opereation = "上传车辆正在审核中";
        }

        sql = "INSERT INTO car(status,name,number,images,price,introduction,address,collection,createtime,gearbox,identity,phone,username,age) VALUES ('"+status+"','"+name+"', '"+number+"', '"+images+"', "+price+", '"+introduction+"', '"+address+"', 0,  '"+createtime+"', "+gearbox+", '"+identity+"', '"+phone+"', '"+username+"',"+age+")";




    }else
    if (state == 2){
        id = Integer.valueOf(request.getParameter("id"));
        price = request.getParameter("price");
        identity = request.getParameter("identity");
        images = request.getParameter("images");
        number = request.getParameter("number");
        introduction = request.getParameter("introduction");
        age =Integer.valueOf( request.getParameter("age"));
        gearbox =Integer.valueOf( request.getParameter("gearbox"));
        name = request.getParameter("name");
        phone = request.getParameter("phone");
        createtime = request.getParameter("createtime");
        collection = Integer.valueOf(request.getParameter("collection"));


        opereation = "修改车辆信息";
        sql = "update car set name='"+name+"',number='"+number+"',images='"+images+"' ,price= "+price+",introduction='"+introduction+"',address='"+introduction+"',collection="+collection+",createtime='"+createtime+"',gearbox= "+gearbox+",identity='"+identity+"',phone='"+phone+"',username='"+username+"',age="+age+" where id ="+id;

    }else if(state == 3){
        id = Integer.valueOf(request.getParameter("id"));
        sql ="delete from car where id ="+id;
        opereation="删除车辆";
    }else  if(state ==7){
        //审核车辆
        id = Integer.valueOf(request.getParameter("id"));
        status = Integer.valueOf(request.getParameter("status"));
        sql = "update car set status ="+status+" where id ="+id;
        opereation="审核操作完成";
    }




    System.out.println("执行了==>"+sql);
    //jdbc连接数据库

    //jdbc 操作
    String url = "jdbc:mysql://localhost:3306/carxianyu";
    String user = "root";
    String pwd = "root";
    int rs = 0;
    Connection conn=null;

        Class.forName("com.mysql.jdbc.Driver");//加载驱动
        conn = DriverManager.getConnection("jdbc:mysql://43.142.239.28:13307/carxianyu?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC","root","root");
        PreparedStatement a = conn.prepareStatement(sql);
        rs = a.executeUpdate(sql);


        String carAdmin = "<script type='text/javascript'>alert('"+opereation+"成功');window.location.href='carList.jsp';</script>";


        String carAddCustomer = "<script type='text/javascript'>alert('"+opereation+"成功');window.location.href='/';</script>";
        String carCustomer = "<script type='text/javascript'>alert('"+opereation+"成功');window.location.href='/view/customer/customer/car/myCarList.jsp';</script>";



        if (rs != 0) {
            //进入管理员列表
            if (depart == 11) {
                out.print(carAdmin);
            } else {
                //进入 用户
                if (state == 1) {
                    out.print(carAddCustomer);
                } else {
                    //删除修改
                   out.println(carCustomer);
                }
            }
        }




%>

















<head>
    <title>处理cru的逻辑</title>
</head>

</html>
